using System;
using System.Collections;
using System.Drawing;
using FunkeLab;


namespace Concurso.Clases.TreeViewManage
{
	/// <summary>
	/// Summary description for TreeViewControl.
	/// </summary>
	public class TreeViewControl : System.Web.UI.UserControl {
		
		// interface
		private MyTreeViewItem _selectedNode;

		#region - Properties -

		public ArrayList SelectedPathNode 
		{
			get 
			{
				return (ArrayList) ViewState["_selectedPathNode"];
			}
			set 
			{
				ViewState["_selectedPathNode"] = value;
			}
		}

		#endregion

		#region Constructores

		public TreeViewControl() {
		}

		#endregion

		#region TreeViewControl Interface
	
		#region - Select -

		public virtual MyTreeViewItem GetSelectedNode() {
			return _selectedNode;	
		}


		/// <summary>
		/// Realiza la seleccion del nodo indicado por el path.
		/// </summary>
		/// <param name="path"></param>
		public virtual void SelectNode (ArrayList path) 
		{
			MyTreeViewItem node = this.Tree.GetNode(path);
			this.PaintSelectedNode(this.Tree, node);
		}

		public virtual void SelectNode (ArrayList path, string Id) 
		{
			MyTreeViewItem node = this.Tree.GetNode(path);
			// agrego la position del nuevo asset en el session path
			ArrayList pathNode = path;
			pathNode.Add( Find(node,Id) );
			SelectedPathNode = pathNode;
			SelectNode( SelectedPathNode );
		}

		private int Find( MyTreeViewItem parent, string Id ) 
		{			
			
			for( int i = 0 ; i < parent.Items.Count ; i++ ) 
			{
				MyTreeViewItem item = parent.Items[i];
				if( item.Value == Id ) 
				{
					return item.Position;
				}			
			}

			return 0;
		}
	

		public ArrayList GetPathWithNewId(ArrayList path, string Id) {
			
			MyTreeViewItem item = Tree.GetNode(path);
			int position = Find( item, Id );
			path.Add(position);
			return path;			
		}

		protected virtual void treeView_OnNodeClick( ref FunkeLab.MyTreeViewItem Item ) {
			
		}
		protected virtual void treeView_OnNodeLoadOnDemand( ref MyTreeViewItem Item ) {
		}
	
		protected virtual MyTreeViewItem RootItem
		{
			get{ return null;}
		}

		public void LoadTreeByPath(string path) 
		{
			string[] pathSplit = path.Split(':');
			if ( pathSplit.Length > 1)
			{
				MyTreeViewItem it = RootItem.Items[ Convert.ToInt32(pathSplit[1]) ];

				for ( int i = 2; i <= pathSplit.Length - 1; i++ )
				{
					if ( it.Items.Count <= 0 )
					{
						treeView_OnNodeLoadOnDemand( ref it );
						it.Open = true;
					}
					it = it.Items[ Convert.ToInt32(pathSplit[i]) ];
				}
				treeView_OnNodeClick( ref it );
			}
			else
			{
				MyTreeViewItem item = RootItem;
				treeView_OnNodeClick( ref item );
			}
		}

		public void SpanTreeByPath( ArrayList path )
		{
			if ( path.Count > 1 )
			{
				MyTreeViewItem it = RootItem.Items[ Convert.ToInt32(path[1]) ];
	
				for ( int i = 2; i <= path.Count; i++ ) 
				{
					if ( it.Items.Count <= 0 ) 
					{
						treeView_OnNodeLoadOnDemand( ref it );
						it.Open = true;
					}
					if( i!=path.Count )
						it = it.Items[ Convert.ToInt32(path[i]) ];
				}
			}
		}

		public string ConvertArrayPathToString( ArrayList itemPath )
		{
			string strPos = string.Empty;
			strPos = itemPath[0].ToString();
			for ( int pos = 1; pos <= itemPath.Count -1; pos++ ) 
			{
				strPos = strPos + ":" + itemPath[pos].ToString();			
			}
			return strPos;
		}

		public void LoadTreeByPath( ArrayList path) 
		{
			string strPath = ConvertArrayPathToString( path );
			LoadTreeByPath( strPath );
		}
	

		public void CloseNode( ArrayList path )
		{
			ArrayList tempPath = new ArrayList();
			for(int i=0; i< path.Count; i++)
			{
				tempPath.Add(path[i]);
				MyTreeViewItem item = Tree.GetNode( tempPath );
				if( !item.Open )
					break;
				if( i == path.Count - 1)
				{
					item.Items.Clear();
					item.Open = false;
				}
			}
		}

		public bool IsSelectedNode( ArrayList path ) {
			ArrayList tempPath = new ArrayList();
			bool IsSelectedNode = true;
			for(int i=0; i< path.Count - 1 ; i++) {
				tempPath.Add(path[i]);
				MyTreeViewItem item = Tree.GetNode( tempPath );
				if( !item.Open )
				{
					IsSelectedNode = false;
					break;	
				}										
			}
			if( IsSelectedNode )
			{
				MyTreeViewItem item = Tree.GetNode( path );
				if( item.Selected )
				{
					IsSelectedNode = true;
				}
			}
			return IsSelectedNode;
		}

		public virtual void UnselectNode (MyTreeViewItem node)
		{
			node.Selected = false;
			//hacer lo que haiga que hacer
		}


		public virtual void UnselectAllNodes ()
		{
				
		}


		protected void SetSelectedNode (MyTreeViewItem node)
		{
			_selectedNode = node;
		}

		public virtual string SetPathLabel
		{
			get{ return null;}
			set{ }
		}		

		public virtual bool PathLabelVisible {
			get{ return false;}
			set{ }
		}		

		#endregion

		public ArrayList GetPathNode(MyTreeViewItem node)
		{
			return this.Tree.GetPathNode(node);
		}
		
		public virtual MyTreeView Tree
		{
			get
			{
				return null;
			}
		}

		#endregion

		#region Aux

/*		protected void LoadNodes( MyTreeViewItem parentItem, IList rootNodes, string type) 
		{
			foreach( NodeDTO node in rootNodes ) {
				MyTreeViewItem item = new MyTreeViewItem();
				item.Text = node.Name;
				item.Value = GetNodeTreeValue(type, node);
				item.LoadOnExpand = true;

				AssingItemImage(type, node, item);

				parentItem.Items.Add( item );
			}
		}

		protected string GetOrganizationRoot(string nodeId) 
		{
			object parentId;
			string text = "";
			OrganizationNodeDTO orgDTO = OrganizationNodeInterface.GetOrganizationNode( nodeId );
			text = orgDTO.Name;
			parentId = orgDTO.Parent;
			while ( parentId != null) {
				orgDTO = OrganizationNodeInterface.GetOrganizationNode( parentId );
				text = orgDTO.Name + " > " + text;
				parentId = orgDTO.Parent;
			}

			return text;
		}

		public int GetActivityLevel( ActivityTypeDTO node)
		{
			int lvl = 0;
			ActivityTypeDTO actTDTO = ActivityTypeInterface.Get( node.Id );
			while ( actTDTO.Parent != null )
			{
				lvl += 1;
				actTDTO = ActivityTypeInterface.Get( actTDTO.Parent );
			}
			return lvl;
		}
*/
		public string GetPathWithNames( ArrayList path ) 
		{
			string pathName = string.Empty;
			ArrayList tempPath = new ArrayList();
			pathName = RootItem.Text;
			tempPath.Add(path[0]);

			for(int i=1; i< path.Count; i++) 
			{				
				tempPath.Add(path[i]);
				MyTreeViewItem item = Tree.GetNode( tempPath );
				pathName += " > " + item.Text;
			}

			return pathName;
		}

		protected void PaintSelectedNode(MyTreeView treeView, MyTreeViewItem Item) 
		{
			string strPos = String.Empty;
			ArrayList itemPath = treeView.GetPathNode( Item );

			strPos = itemPath[0].ToString();
			for ( int pos = 1; pos <= itemPath.Count -1; pos++ ) {
				strPos = strPos + ":" + itemPath[pos].ToString();
			}

			if ( ViewState["SelectedNodePath"] != null ) {
				if ( ViewState["SelectedNodePath"].ToString() != strPos ) {
					string[] oldPath = ViewState["SelectedNodePath"].ToString().Split(':');
					ArrayList oldPathList = new ArrayList( oldPath.Length );
					foreach ( string str in oldPath ) {
						oldPathList.Add( Convert.ToInt32( str ) );
					}
					try {
						MyTreeViewItem it = treeView.GetNode( oldPathList );
						it.BackColor = System.Drawing.Color.White;
					}
					catch ( Exception ex ) {
						string error = ex.ToString();
					}
					ViewState.Add( "SelectedNodePath", strPos );
					Item.BackColor = System.Drawing.Color.LightBlue;
				}
				else {
					ViewState.Add( "SelectedNodePath", strPos );
					Item.BackColor = System.Drawing.Color.LightBlue;					
				}
			}
			else {
				ViewState.Add( "SelectedNodePath", strPos );
				Item.BackColor = System.Drawing.Color.LightBlue;
			}
		}

		protected void PaintSelectedNode( MyTreeView treeView, MyTreeViewItem Item, Color color)
		{
			ArrayList itemPath = treeView.GetPathNode( Item );

			string strPos = ConvertArrayPathToString( itemPath );

			if ( ViewState["SelectedNodePath"] != null )
			{
				if ( ViewState["SelectedNodePath"].ToString() != strPos )
				{
					ClearPaintedNode( treeView, ViewState["SelectedNodePath"].ToString() );

					ViewState.Add( "SelectedNodePath", strPos );
					Item.BackColor = color;
				}
				else
				{
					ViewState.Add( "SelectedNodePath", strPos );
					Item.BackColor = color;
				}
			}
			else
			{
				ViewState.Add( "SelectedNodePath", strPos );
				Item.BackColor = color;
			}
		}

		private void ClearPaintedNode( MyTreeView treeView, string path)
		{
			string[] oldPath = path.Split(':');
			ArrayList oldPathList = new ArrayList( oldPath.Length );
			foreach ( string str in oldPath )
			{
				oldPathList.Add( Convert.ToInt32( str ) );
			}
	
			try
			{
				MyTreeViewItem it = treeView.GetNode( oldPathList );
				it.BackColor = Color.White;
			}
			catch
			{
			}
		}

		protected virtual void SaveSelectedNode(string nodeType, string nodeId, string assetId) 
		{
		
		}

		#endregion
	}
}
